package com.taojie.graduation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taojie.graduation.domain.Product;
import com.taojie.graduation.mapper.ProductMapper;
import com.taojie.graduation.service.api.ProductService;
import com.taojie.graduation.utils.PageResult;
import com.taojie.graduation.utils.Result;
import com.taojie.graduation.utils.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    ProductMapper productMapper;

    /**
     * TODO 分页查
     * @param page
     * @param size
     * @param query
     * @return
     */
    @Override
    public Result findProductPage(int page, int size, String query) {
        Page<Product> userPage = new Page<>(page, size);
        QueryWrapper<Product> wapper = null;
        if (!StringUtils.isEmpty(query)) {
            wapper = new QueryWrapper<>();
            wapper.like("name", query);
        }
        Page<Product> productPage1 = productMapper.selectPage(userPage, wapper);
        // 根据id排序
        List<Product> collect = productPage1.getRecords().stream().sorted(Comparator.comparing(Product::getId)).collect(Collectors.toList());
        if (!(productPage1.getRecords().size() > 0)) {
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS, new PageResult<Product>(productPage1.getTotal(), collect));
    }

    /**
     * TODO 根据id查
     * @param id
     * @return
     */
    @Override
    public Result findProductById(Integer id) {
        if (id < 0) { return  Result.INPUT_ERROR(); }
        Product product = productMapper.selectById(id);
        if (ObjectUtils.isEmpty(product)){
            return Result.FAIL();
        }
        return new Result(ResultCode.SUCCESS, product);
    }

    /**
     * TODO 更新
     * @param product
     * @return
     */
    @Override
    public Result updateById(Product product) {
        if (product.getId() < 0) {
            return Result.INPUT_ERROR();
        }
        int i = productMapper.updateById(product);
        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        if (id < 0) {
            return Result.INPUT_ERROR();
        }
        int i = productMapper.deleteById(id);
        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 新增
     * @param product
     * @return
     */
    @Override
    public Result add(Product product) {
        if (StringUtils.isEmpty(product.getName())) {
            return Result.INPUT_ERROR();
        }
        int i = productMapper.insert(product);
        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }
}
